package com.dream.usercenter.application.controller;

import com.dream.usercenter.application.service.IUcSettingsService;
import com.dream.usercenter.base.result.ResultModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dream.usercenter.domain.service.query.UcSettingsQueryBo;
import com.dream.usercenter.application.client.req.UcSettingsReq;
import com.dream.usercenter.application.client.resp.UcSettingsResp;
import com.dream.usercenter.domain.persistence.po.UcSettings;
import java.util.ArrayList;
import java.util.List;
import com.dream.usercenter.domain.vo.resp.PageVOResp;
import com.dream.usercenter.domain.vo.req.UcSettingsPageVOReq;
import com.dream.usercenter.application.client.req.vo.UcSettingsEntityVOReq;

/**
 * <p>
 * 配置信息  前端控制器 由模板自动生成
 * </p>
 *
 * @author liuyj
 * @since 2022-07-23 19:01:30
 */
@Controller
@RequestMapping("/ucsettings")
public class UcSettingsController {

    @Autowired
    private IUcSettingsService iUcSettingsService;

    /**
    * 表格页面
    */
    @RequestMapping({"tables"})
    public String tables() {
        return "tables/ucSettings";
    }

    /**
     * 添加对象
     *
     * @param ucSettingsEntityVOReq 对象
     * @return ResultModel统一响应结果
     */
    @PostMapping("save")
    @ResponseBody
    public ResultModel<Object> save(@RequestBody UcSettingsEntityVOReq ucSettingsEntityVOReq) {
        if(ucSettingsEntityVOReq==null){
            return ResultModel.parameterError();
        }
        UcSettings ucSettings = getUcSettings(ucSettingsEntityVOReq);
        iUcSettingsService.save(ucSettings);
        return ResultModel.success();
    }

    /**
     * 根据ID进行删除
     *
     * @param id 主键
     * @return ResultModel统一响应结果
     */
    @GetMapping("removeById")
    @ResponseBody
    public ResultModel<Object> delete(@RequestParam Integer id) {
        if(id==null || id<0){
            return ResultModel.parameterError();
        }
        boolean success = iUcSettingsService.removeById(id);
        if(success) {
            return ResultModel.success();
        }else {
            return ResultModel.fail();
        }
    }

    /**
     * 根据ID进行修改对象
     *
     * @param ucSettingsEntityVOReq 对象中必须有ID主键
     * @return ResultModel统一响应结果
     */
    @PostMapping("update")
    @ResponseBody
    public ResultModel<Object> update(@RequestBody UcSettingsEntityVOReq ucSettingsEntityVOReq) {
        if(ucSettingsEntityVOReq==null || ucSettingsEntityVOReq.getId()==null){
            return ResultModel.parameterError();
        }
        UcSettings ucSettings = getUcSettings(ucSettingsEntityVOReq);
        boolean success = iUcSettingsService.updateById(ucSettings);
        if(success) {
            return ResultModel.success();
        }else {
            return ResultModel.fail();
        }
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return ResultModel统一响应结果
     */
    @GetMapping("getById")
    @ResponseBody
    public ResultModel<UcSettingsResp> getById(@RequestParam Integer id) {
        if(id==null || id<0){
            return ResultModel.parameterError();
        }
        UcSettings ucSettings= iUcSettingsService.getById(id);
        UcSettingsResp ucSettingsResp = new UcSettingsResp();
        BeanUtils.copyProperties(ucSettings,ucSettingsResp);
        return ResultModel.success(ucSettingsResp);
    }

    /**
     * 分页查询
     *
     * @return ResultModel统一响应结果
     */
    @PostMapping("page")
    @ResponseBody
    public ResultModel<PageVOResp<UcSettingsResp>> page(@RequestBody UcSettingsPageVOReq pageVOReq) {
        IPage<UcSettings> iPage = iUcSettingsService.page(pageVOReq.getQueryBo(),pageVOReq.getPageNo(),pageVOReq.getPageSize());
        PageVOResp<UcSettingsResp> templateIPage = new PageVOResp<>();
        templateIPage.setPages(iPage.getPages());
        templateIPage.setCurrent(iPage.getCurrent());
        templateIPage.setSize(iPage.getSize());
        templateIPage.setTotal(iPage.getTotal());
        List<UcSettings> ucSettingsList = iPage.getRecords();
        List<UcSettingsResp> ucSettingsRespList = new ArrayList<>(ucSettingsList.size());
        for (UcSettings ucSettings : ucSettingsList) {
            UcSettingsResp ucSettingsResp = new UcSettingsResp();
            BeanUtils.copyProperties(ucSettings,ucSettingsResp);
            ucSettingsRespList.add(ucSettingsResp);
        }
        templateIPage.setDraw(pageVOReq.getDraw());
        templateIPage.setRecords(ucSettingsRespList);
        return ResultModel.success(templateIPage);
    }


    public UcSettings getUcSettings(UcSettingsEntityVOReq req){
        UcSettings entity = new UcSettings();
                                                            if(req.getId()!=null) {
                    entity.setId(req.getId());
                }
                                                                        if(req.getSetName()!=null && req.getSetName().length()>0) {
                    entity.setSetName(req.getSetName());
                }
                                                                        if(req.getSetCode()!=null && req.getSetCode().length()>0) {
                    entity.setSetCode(req.getSetCode());
                }
                                                                        if(req.getSetValue()!=null && req.getSetValue().length()>0) {
                    entity.setSetValue(req.getSetValue());
                }
                                                                        if(req.getDesc()!=null && req.getDesc().length()>0) {
                    entity.setDesc(req.getDesc());
                }
                                                                        if(req.getPlatId()!=null) {
                    entity.setPlatId(req.getPlatId());
                }
                                                                        if(req.getCreateDateTime()!=null) {
                    entity.setCreateDateTime(req.getCreateDateTime());
                }
                                                                        if(req.getUpdateDateTime()!=null) {
                    entity.setUpdateDateTime(req.getUpdateDateTime());
                }
                                                                        if(req.getCreateDate()!=null) {
                    entity.setCreateDate(req.getCreateDate());
                }
                            return entity;
    }

}
